﻿using System;
using System.Collections.Generic;
using System.Drawing;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YRevitJig
{
	public class JigFuncs
	{
		public static short LOWORD(int value)
		{
			return (short)(value & 65535);
		}

		public static short HIWORD(int value)
		{
			return (short)(value >> 16);
		}

		public static XYZ GetProjectPoint(UIDocument UIDoc, XYZ ptUser, XYZ ptOrigin, double dLineAngle)
		{
			Autodesk.Revit.DB.View activeView = UIDoc.ActiveView;
			Transform transform = Transform.CreateRotation(activeView.ViewDirection, dLineAngle);
			XYZ xyz = transform.OfVector(activeView.RightDirection);
			Line line = Line.CreateUnbound(ptOrigin, xyz);
			XYZ xyz2 = JigFuncs.PointToPlane(ptOrigin, activeView.ViewDirection, ptUser);
			return line.Project(xyz2).XYZPoint;
		}

		public static XYZ GetOrthoPoint(UIDocument UIDoc, XYZ ptUser, XYZ ptOrigin)
		{
			Autodesk.Revit.DB.View activeView = UIDoc.ActiveView;
			Line line = Line.CreateUnbound(ptOrigin, activeView.RightDirection);
			Line line2 = Line.CreateUnbound(ptOrigin, activeView.UpDirection);
			XYZ xyz = JigFuncs.PointToPlane(ptOrigin, activeView.ViewDirection, ptUser);
			XYZ xyzpoint = line.Project(xyz).XYZPoint;
			XYZ xyzpoint2 = line2.Project(xyz).XYZPoint;
			bool flag = xyz.DistanceTo(xyzpoint) < xyz.DistanceTo(xyzpoint2);
			XYZ result;
			if (flag)
			{
				result = xyzpoint;
			}
			else
			{
				result = xyzpoint2;
			}
			return result;
		}

		public static XYZ GetAxisPoint(UIDocument UIDoc, XYZ ptUser, XYZ ptOrigin)
		{
			XYZ xyz = JigFuncs.PointToPlane(ptOrigin, UIDoc.ActiveView.ViewDirection, ptUser);
			bool flag = xyz.DistanceTo(ptOrigin) < 0.0032808398950131233;
			XYZ result;
			if (flag)
			{
				result = null;
			}
			else
			{
				Autodesk.Revit.DB.View activeView = UIDoc.ActiveView;
				Line line = Line.CreateUnbound(ptOrigin, activeView.RightDirection);
				XYZ xyzpoint = line.Project(xyz).XYZPoint;
				bool flag2 = xyzpoint.DistanceTo(ptOrigin) > 0.1 && JigFuncs.Get3PtAngle(xyzpoint, ptOrigin, xyz) < 0.017453292519943295;
				if (flag2)
				{
					result = xyzpoint;
				}
				else
				{
					line = Line.CreateUnbound(ptOrigin, activeView.UpDirection);
					xyzpoint = line.Project(xyz).XYZPoint;
					bool flag3 = xyzpoint.DistanceTo(ptOrigin) > 0.1 && JigFuncs.Get3PtAngle(xyzpoint, ptOrigin, xyz) < 0.017453292519943295;
					if (flag3)
					{
						result = xyzpoint;
					}
					else
					{
						Transform transform = Transform.CreateRotation(activeView.ViewDirection, 0.78539816339744828);
						XYZ xyz2 = transform.OfVector(activeView.RightDirection);
						line = Line.CreateUnbound(ptOrigin, xyz2);
						xyzpoint = line.Project(xyz).XYZPoint;
						bool flag4 = xyzpoint.DistanceTo(ptOrigin) > 0.1 && JigFuncs.Get3PtAngle(xyzpoint, ptOrigin, xyz) < 0.017453292519943295;
						if (flag4)
						{
							result = xyzpoint;
						}
						else
						{
							xyz2 = transform.OfVector(activeView.UpDirection);
							line = Line.CreateUnbound(ptOrigin, xyz2);
							xyzpoint = line.Project(xyz).XYZPoint;
							bool flag5 = xyzpoint.DistanceTo(ptOrigin) > 0.1 && JigFuncs.Get3PtAngle(xyzpoint, ptOrigin, xyz) < 0.017453292519943295;
							if (flag5)
							{
								result = xyzpoint;
							}
							else
							{
								result = null;
							}
						}
					}
				}
			}
			return result;
		}

		public static double Get3PtAngle(XYZ pt1, XYZ ptOrigin, XYZ pt2)
		{
			XYZ xyz = pt1 - ptOrigin;
			XYZ xyz2 = pt2 - ptOrigin;
			return xyz.AngleTo(xyz2);
		}

		public static bool Screen2Client(IntPtr hWnd, ref System.Drawing.Point pt)
		{
			return DllImportManeger.ScreenToClient(hWnd, ref pt);
		}

		public static bool Client2Screen(IntPtr hWnd, ref System.Drawing.Point pt)
		{
			return DllImportManeger.ClientToScreen(hWnd, ref pt);
		}

		public static double RevitToScreenScale(UIView uiView, Autodesk.Revit.DB.View view)
		{
			IList<XYZ> zoomCorners = uiView.GetZoomCorners();
			XYZ xyz = zoomCorners[0];
			XYZ xyz2 = zoomCorners[1];
			Transform inverse = view.CropBox.Transform.Inverse;
			xyz = inverse.OfPoint(xyz);
			xyz2 = inverse.OfPoint(xyz2);
			int num = 0;
			int num2 = 0;
			int num3 = 0;
			int num4 = 0;
			RevitViewExtension.GetRevitWndRectangle(uiView, ref num, ref num2, ref num3, ref num4);
			return (xyz2.Y - xyz.Y) / (double)(num4 - num2);
		}

		public static XYZ PointToPlane(XYZ ptPlaneOrigin, XYZ vtPlane, XYZ ptUser)
		{
			Line line = Line.CreateUnbound(ptUser, vtPlane);
			return line.Project(ptPlaneOrigin).XYZPoint;
		}
	}
}
